package imsdk

import (
	"fmt"
	"github.com/google/uuid"
	"longmen/server/config/global"
	"net/url"
	"sync"
)

//

type sdk struct {
	domain     string
	appId      string
	userSig    string
	OfflineMsg OfflineMsg
}

var (
	once     sync.Once
	instance *sdk
)

func GetSdk() *sdk {
	once.Do(func() {
		domain := global.Config.GetString("tencent_im.api_url")
		appApi := global.Config.GetString("tencent.im.sdk.app.id")
		userSig := global.Config.GetString("tencent_im.usersig")
		instance = &sdk{
			domain:  domain,
			appId:   appApi,
			userSig: userSig,
		}
	})
	return instance
}

func (s sdk) BuildRestfulUrl(reqUrl string, args ...map[string]interface{}) string {
	queryArgs := map[string]interface{}{
		"sdkappid":    s.appId,
		"identifier":  "admin",
		"usersig":     s.userSig,
		"contenttype": "json",
		"random":      uuid.New().String(),
	}
	var uri url.URL
	query := uri.Query()
	for key, val := range queryArgs {
		query.Add(key, val.(string))
	}
	for _, arg := range args {
		for k, v := range arg {
			query.Add(k, fmt.Sprintf("%v", v))
		}
	}
	return s.domain + reqUrl + "?" + query.Encode()
}
